﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using personalplaner.common.converters;

namespace personalplaner.common.controls.primitives
{
    public class DiensteCellTemplateSelector : DataTemplateSelector
    {
        //ColorToSolidColorBrushConverter _colorToBrushConverter;
        //private ColorToSolidColorBrushConverter ColorToBrushConverter
        //{
        //    get
        //    {
        //        if (_colorToBrushConverter == null)
        //            _colorToBrushConverter = new ColorToSolidColorBrushConverter();
        //        return _colorToBrushConverter;
        //    }
        //}

        DataTemplate _cellTemplate;
        private DataTemplate GetCellTemplate(FrameworkElement container)
        {
            if (_cellTemplate == null)
            {
                _cellTemplate = container.FindResource("PART_CellTemplate") as DataTemplate;
            }

            return _cellTemplate;
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var fe = container as FrameworkElement;

            if (fe != null)
            {
                
                //var grid = new FrameworkElementFactory(typeof(Grid));

                //var backgroundBorder = new FrameworkElementFactory(typeof(Border));
                //var backgroundBinding = new Binding("BackgroundColor");
                //backgroundBinding.Converter = ColorToBrushConverter;
                //backgroundBorder.SetBinding(Border.BackgroundProperty, backgroundBinding);

                //grid.AppendChild(backgroundBorder);


                //var secondBorder = new FrameworkElementFactory(typeof(Border));
                //var secondBorderBinding = new Binding("Color");
                //secondBorderBinding.Converter = ColorToBrushConverter;
                //secondBorder.SetBinding(Border.BackgroundProperty, secondBorderBinding);

                //var button = new FrameworkElementFactory(typeof(Button));
                //secondBorder.AppendChild(button);

                //var textBlock = new FrameworkElementFactory(typeof(TextBlock));
                //textBlock.SetBinding(TextBlock.TextProperty, new Binding("Name"));
                //button.AppendChild(textBlock);

                //grid.AppendChild(secondBorder);



                //var template = new DataTemplate();
                //template.VisualTree = grid;

                //return template;

                /*
                <Grid>
                    <Border Background="{Binding BackgroundColor, Converter={StaticResource colorToBrush}}" Focusable="False"/>
                    <Border Background="{Binding Color, Converter={StaticResource colorToBrush}}">
                        <Button Style="{StaticResource TransparentButtonStyle}" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:PlanGrid}, AncestorLevel=1}, Path=Command}" CommandParameter="{Binding}" >
                            <TextBlock Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2,1,2,1" FontWeight="DemiBold" FontSize="10"/>
                        </Button>
                    </Border>
                </Grid>
                */

                return GetCellTemplate(fe);

            }
            return base.SelectTemplate(item, container);
        }
    }
}
